{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "veterinary-mitchell",
   "metadata": {},
   "source": [
    "# SEPA Identifier of the Creditor"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "caring-membership",
   "metadata": {},
   "source": [
    "## Introduction"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "considerable-minnesota",
   "metadata": {},
   "source": [
    "The function `clean_eu_at_02()` cleans a column containing AT-02 (SEPA Creditor identifier) strings, and standardizes them in a given format. The function `validate_eu_at_02()` validates either a single AT-02 strings, a column of AT-02 strings or a DataFrame of AT-02 strings, returning `True` if the value is valid, and `False` otherwise."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "numerical-exposure",
   "metadata": {},
   "source": [
    "AT-02 strings can be converted to the following formats via the `output_format` parameter:\n",
    "\n",
    "* `compact`: only number strings without any seperators or whitespace, like \"ES23ZZZ47690558N\"\n",
    "* `standard`: AT-02 strings with proper whitespace in the proper places. Note that in the case of AT-02, the compact format is the same as the standard one.\n",
    "\n",
    "Invalid parsing is handled with the `errors` parameter:\n",
    "\n",
    "* `coerce` (default): invalid parsing will be set to NaN\n",
    "* `ignore`: invalid parsing will return the input\n",
    "* `raise`: invalid parsing will raise an exception\n",
    "\n",
    "The following sections demonstrate the functionality of `clean_eu_at_02()` and `validate_eu_at_02()`. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "catholic-devil",
   "metadata": {},
   "source": [
    "### An example dataset containing AT-02 strings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "maritime-victory",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "df = pd.DataFrame(\n",
    "    {\n",
    "        \"eu_at_02\": [\n",
    "            'ES++()+23ZZZ4//7690558N',\n",
    "            'ES2900047690558N',\n",
    "            'BE 428759497',\n",
    "            'BE431150351',\n",
    "            \"002 724 334\",\n",
    "            \"hello\",\n",
    "            np.nan,\n",
    "            \"NULL\",\n",
    "        ], \n",
    "        \"address\": [\n",
    "            \"123 Pine Ave.\",\n",
    "            \"main st\",\n",
    "            \"1234 west main heights 57033\",\n",
    "            \"apt 1 789 s maple rd manhattan\",\n",
    "            \"robie house, 789 north main street\",\n",
    "            \"1111 S Figueroa St, Los Angeles, CA 90015\",\n",
    "            \"(staples center) 1111 S Figueroa St, Los Angeles\",\n",
    "            \"hello\",\n",
    "        ]\n",
    "    }\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "removed-renewal",
   "metadata": {},
   "source": [
    "## 1. Default `clean_eu_at_02`\n",
    "\n",
    "By default, `clean_eu_at_02` will clean eu_at_02 strings and output them in the standard format with proper separators."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "activated-dragon",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import clean_eu_at_02\n",
    "clean_eu_at_02(df, column = \"eu_at_02\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "selective-scottish",
   "metadata": {},
   "source": [
    "## 2. Output formats"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "three-technology",
   "metadata": {},
   "source": [
    "This section demonstrates the output parameter."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bigger-lodging",
   "metadata": {},
   "source": [
    "### `standard` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "blessed-poverty",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_eu_at_02(df, column = \"eu_at_02\", output_format=\"standard\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "latest-czech",
   "metadata": {},
   "source": [
    "### `compact`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "utility-forest",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_eu_at_02(df, column = \"eu_at_02\", output_format=\"compact\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "abstract-northeast",
   "metadata": {},
   "source": [
    "## 3. `inplace` parameter\n",
    "\n",
    "This deletes the given column from the returned DataFrame. \n",
    "A new column containing cleaned AT-02 strings is added with a title in the format `\"{original title}_clean\"`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "pursuant-contributor",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_eu_at_02(df, column=\"eu_at_02\", inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "vital-recovery",
   "metadata": {},
   "source": [
    "## 4. `errors` parameter"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "amber-domestic",
   "metadata": {},
   "source": [
    "### `coerce` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "military-cherry",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_eu_at_02(df, \"eu_at_02\", errors=\"coerce\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "occupied-portrait",
   "metadata": {},
   "source": [
    "### `ignore`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "flush-sweden",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_eu_at_02(df, \"eu_at_02\", errors=\"ignore\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "honest-supervisor",
   "metadata": {},
   "source": [
    "## 4. `validate_eu_at_02()`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "strange-reasoning",
   "metadata": {},
   "source": [
    "`validate_eu_at_02()` returns `True` when the input is a valid AT-02. Otherwise it returns `False`.\n",
    "\n",
    "The input of `validate_eu_at_02()` can be a string, a Pandas DataSeries, a Dask DataSeries, a Pandas DataFrame and a dask DataFrame.\n",
    "\n",
    "When the input is a string, a Pandas DataSeries or a Dask DataSeries, user doesn't need to specify a column name to be validated. \n",
    "\n",
    "When the input is a Pandas DataFrame or a dask DataFrame, user can both specify or not specify a column name to be validated. If user specify the column name, `validate_eu_at_02()` only returns the validation result for the specified column. If user doesn't specify the column name, `validate_eu_at_02()` returns the validation result for the whole DataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "sonic-picture",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import validate_eu_at_02\n",
    "print(validate_eu_at_02('ES++()+23ZZZ4//7690558N'))\n",
    "print(validate_eu_at_02('ES2900047690558N'))\n",
    "print(validate_eu_at_02('BE 428759497'))\n",
    "print(validate_eu_at_02('BE431150351'))\n",
    "print(validate_eu_at_02(\"004085616\"))\n",
    "print(validate_eu_at_02(\"hello\"))\n",
    "print(validate_eu_at_02(np.nan))\n",
    "print(validate_eu_at_02(\"NULL\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "recovered-father",
   "metadata": {},
   "source": [
    "### Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "empty-switch",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_eu_at_02(df[\"eu_at_02\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "juvenile-artist",
   "metadata": {},
   "source": [
    "### DataFrame + Specify Column"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "domestic-campaign",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_eu_at_02(df, column=\"eu_at_02\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "respected-doctrine",
   "metadata": {},
   "source": [
    "### Only DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "forty-network",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "validate_eu_at_02(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "improving-texas",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
